package com.example.demo.程序员面试金典;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @author：zzc
 * @date: 2022/9/8
 */
public class 化栈为队 {

    // 输入栈 输出栈
    // 输入直接push
    // 输出 先看输出栈有没有元素 没有则将输入栈得值先peek到输出栈  再从输出栈pop
    private Deque<Integer> inStack;
    private Deque<Integer> outStack;


    /**
     * Initialize your data structure here.
     */
    public 化栈为队() {
        inStack = new LinkedList<>();
        outStack = new LinkedList<>();

    }

    /**
     * Push element x to the back of queue.
     */
    public void push(int x) {
        inStack.push(x);
    }

    /**
     * Removes the element from in front of queue and returns that element.
     */
    public int pop() {
        if (!outStack.isEmpty()) {
            return outStack.pop();
        }
        inToOut();
        return outStack.pop();
    }

    private void inToOut() {
        while (!inStack.isEmpty()) {
            outStack.push(inStack.pop());
        }

    }

    /**
     * Get the front element.
     */
    public int peek() {
        if (!outStack.isEmpty()) {
            return outStack.peek();
        }
        inToOut();
        return outStack.peek();

    }

    /**
     * Returns whether the queue is empty.
     */
    public boolean empty() {
        return outStack.isEmpty();
    }


}
